home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 2.7 KB | 115 lines |
- import java.awt.Point;
-
- /**
- * Apple Worldwide Developer Technical Support
- *
- * A simple example of how to obtain the mouse location using the Mac OS Toolbox through JDirect 2.
- *
- * File: JDirectMouse.java
- *
- *
- * @author Levi Brown
- * @author Apple Computer, Inc.
- *
- * Copyright ©1999 Apple Computer, Inc.
- * All rights reserved.
- *
- * @version 1.0
- * 4/15/1999 Shipped as 'JDirectMouse' sample.
- *
- * You may incorporate this sample code into your applications without
- * restriction, though the sample code has been provided "AS IS" and the
- * responsibility for its operation is 100% yours. However, what you are
- * not permitted to do is to redistribute the source as "Apple Sample
- * Code" after having made changes. If you're going to re-distribute the
- * source, we require that you make it clear in the source that the code
- * was descended from Apple Sample Code, but that you've made changes.
- */
- public class JDirectMouse
- {
- protected boolean isRun;
- protected Thread thread;
-
- /**
- * The entry point for this program
- */
- static public void main(String args[])
- {
- (new JDirectMouse()).start();
- }
-
- /**
- * Constructs a new JDirectMouse object and initializes defaults.
- */
- public JDirectMouse()
- {
- isRun = false;
- thread = null;
- }
-
- /**
- * Obtains the mouse location by calling the Mac OS toolbox call GetMouse.
- * @return the location of the mouse in coordinates local to the current native grafPort.
- */
- public Point getMouseLocation()
- {
- //Create a new Point structure to be populated by the native call.
- PointStruct mouseLoc = new PointStruct();
- //Call the toolbox for the mouse location
- EventFunctions.GetMouse(mouseLoc);
- //Convert the native stucture to a java.awt.Point object, and return it.
- return new Point (mouseLoc.getH(), mouseLoc.getV());
- }
-
- /**
- * Starts the thread to poll the mouse location and print it out to the console.
- * @see #stop
- */
- public void start()
- {
- if (thread == null || !thread.isAlive())
- {
- thread = new Thread(new Task());
- isRun = true;
- thread.start();
- }
- }
-
- /**
- * Stops the thread from polling the mouse location and printing it out to the console.
- * @see #start
- */
- public void stop()
- {
- isRun = false;
- }
-
- /**
- * An inner class containing the body of the thread to poll the mouse.
- */
- public class Task implements Runnable
- {
- public void run()
- {
- Point oldLoc = new Point();
- Point newLoc;
-
- while (isRun)
- {
- try
- {
- newLoc = getMouseLocation();
- if ( !oldLoc.equals(newLoc))
- {
- oldLoc = newLoc;
- System.out.println(newLoc.toString());
- }
-
- //Increase time if race conditions occur.
- Thread.sleep(0);
- }
- catch (InterruptedException exc) { }
- }
- }
- }
- }